"""
Created on 2011-06-02

@author: Olivier
"""
import Core
import types

class Link(object):
    """
    classdocs
    """
    END_A = "A"
    END_B = "B"
    
    _genericString = "Generic Link Name "
    
    _genericNameList = list()
    _name = ""
    #Must be of type System when not none
    _endA = None
    #Must be of type System when not none
    _endB = None
    _direction = Core.Direction.Direction()
    
    def __init__(self, name = "", direction = Core.Direction.Direction()):
        """
        """
        #Set name
        if not name:
            self._generateName()
        else:
            self.setName(name)
        #Set direction
        self.setDirection(direction)
        
    def __del__(self):
        """
        """
        #delete generic names
        if self._genericString in self._name:
            self._name = self._name.replace(self._genericString, "")
            self._genericNameList.remove(int(self._name))
    
    def _generateName(self):
        """
        """
        if len(self._genericNameList) == 0:
            self._name = self._genericString + "1"
            self._genericNameList.append(1)
        else:
            self._name = self._genericString + str(self._genericNameList[-1] + 1)
            self._genericNameList.append(self._genericNameList[-1] + 1)
        #Sort list to have kind of an order
        self._genericNameList.sort()
        
    def connect(self, connection = Core.Connection.Connection()):
        """
        """
        if type(connection) != type(Core.Connection.Connection):
            raise ValueError("connection must be a Connection but is " + str(type(connection)))
        if self._endA is None:
            self._endA = connection
        elif self._endB is None:
            self._endB = connection
        else:
            raise Exception("All ends are occupied")
        
    def unconnect(self, end):
        """
        """
        if end == self.END_A:
            self._endA = None
        elif end == self.END_B:
            self._endB = None
        else:
            raise ValueError("Unknown end specified: " + str(end))
        
    def getEnd(self, end):
        """
        """
        if end == self.END_A:
            return self._endA
        elif end == self.END_B:
            return self._endB
        else:
            raise ValueError("Unknown end specified: " + str(end))
        
    def setName(self, name):
        """
        """
        if type(name) is not types.StringType:
            raise ValueError("name must be a string but is " + str(type(name)))
        if self._genericString in self._name:
            self._name = self._name.replace(self._genericString, "")
            self._genericNameList.remove(int(self._name))
        self._name = name
        
    def setDirection(self, direction = Core.Direction.Direction()):
        """
        """
        if type(direction) == types.StringType:
            self._direction = Core.Direction.Direction(direction)
        elif type(direction) == type(Core.Direction.Direction):
            self._direction = direction
        else:
            raise ValueError("Wrong type for direction")
        
    def getDirection(self):
        return self._direction.getDirection()
